package gov.child.service.impl;

import gov.child.dao.PersonalDeclareFormDao;
import gov.child.entities.po.EDepartment;
import gov.child.entities.po.EPersonalDeclareForm;
import gov.child.entities.po.EUser;
import gov.child.entities.vo.*;
import gov.child.service.PersonalDeclareFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author fzh
 * @since 2017/9/14
 */
@Service("personalDeclareFormService")
public class PersonalDeclareFormServiceImpl implements PersonalDeclareFormService {

    @Autowired
    private PersonalDeclareFormDao personalDeclareFormDao;

    @Override
    public PersonalDeclareForm findById(Integer id) {
        EPersonalDeclareForm form = personalDeclareFormDao.findById(id);
        if (Objects.isNull(form)) return PersonalDeclareForm.EMPTY_PERSONAL_DECLARE_FORM;
        return PersonalDeclareForm.parseFrom(form);
    }

    @Override
    public PersonalDeclareForm findByUnicode(String code) {
        EPersonalDeclareForm form = personalDeclareFormDao.findByUniformCode(code);
        if (Objects.isNull(form)) return PersonalDeclareForm.EMPTY_PERSONAL_DECLARE_FORM;
        return PersonalDeclareForm.parseFrom(form);
    }

    @Override
    public List findByUser(User user) {
        EUser u = User.parseTo(user);
        return BeanTransformer.parseFrom(PersonalDeclareForm.class, personalDeclareFormDao.findByUser(u));
    }

    @Override
    public List findByDepartment(Department department, Page page) {
        EDepartment dept = Department.parseTo(department);
        List l = personalDeclareFormDao.findByDepartment(dept, page);
        return BeanTransformer.parseFrom(PersonalDeclareForm.class, l);
    }

    @Override
    public List findByDepartmentAndStatus(Department department, byte status, Page page) {
        EDepartment dept = Department.parseTo(department);
        List temp = personalDeclareFormDao.findByDepartment(dept, page);
        List l = (List) temp.stream().filter(o -> ((EPersonalDeclareForm) o).getStatus() == status).collect(Collectors.toList());
        return BeanTransformer.parseFrom(PersonalDeclareForm.class, l);
    }

    @Override
    public boolean save(PersonalDeclareForm personalDeclareForm) {
        EPersonalDeclareForm form = PersonalDeclareForm.parseTo(personalDeclareForm);
        personalDeclareFormDao.save(form);
        return true;
    }

    @Override
    public boolean update(PersonalDeclareForm personalDeclareForm) {
        EPersonalDeclareForm form = PersonalDeclareForm.parseTo(personalDeclareForm);
        personalDeclareFormDao.update(form);
        return true;
    }

    @Override
    public boolean delete(PersonalDeclareForm personalDeclareForm) {
        EPersonalDeclareForm form = PersonalDeclareForm.parseTo(personalDeclareForm);
        personalDeclareFormDao.delete(form);
        return true;
    }

    @Override
    public Page getPage(int pageSize) {
        long total = personalDeclareFormDao.total();
        return PageUtil.createPage(pageSize, total, 1);
    }
}
